﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;
using JsonSerializer = Newtonsoft.Json.JsonSerializer;


namespace treeView.Config
{
    public static class JsonOperation
    {
        public static T ReadConfigFromFile<T>(string path)
        {
            using (var file = File.OpenText(path))
            {
                var serializer = new JsonSerializer();
                return serializer.Deserialize<T>(new JsonTextReader(file));
            }
        }

        public static void WriteToJsonFile<T>(T obj, string filePath)
        {
            if (File.Exists(filePath))
                File.Delete(filePath);
            var path = filePath.Replace(filePath.Split("\\").Last(), "");
            Directory.CreateDirectory(path);
            using (var file = new StreamWriter(File.OpenWrite(filePath)))
            {
                JsonSerializerSettings jsetting = new JsonSerializerSettings();
                jsetting.DefaultValueHandling = DefaultValueHandling.Include;
                file.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, jsetting));
            }
        }
    }
}
